package com.example.imserver.controller;

import com.example.imserver.entity.Contact;
import com.example.imserver.service.ContactService;
import com.example.imserver.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/contacts")
public class ContactController {
    @Autowired
    private ContactService contactService;
    
    private Long getUserIdFromRequest(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
            return JwtUtil.getUserIdFromToken(token);
        }
        return null;
    }
    
    @GetMapping("/list")
    public Map<String, Object> getContacts(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        List<Map<String, Object>> contactsWithInfo = contactService.getUserContactsWithInfo(userId);
        result.put("success", true);
        result.put("contacts", contactsWithInfo);
        return result;
    }
    
    @PostMapping("/add")
    public Map<String, Object> addContact(@RequestBody Map<String, String> requestBody, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        String qrCode = requestBody.get("qrCode");
        return contactService.addContactRequest(userId, qrCode);
    }
    
    @PutMapping("/{requestId}/accept")
    public Map<String, Object> acceptContactRequest(@PathVariable Long requestId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        try {
            contactService.acceptContactRequest(requestId, userId);
            result.put("success", true);
            result.put("message", "已接受请求");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    @DeleteMapping("/{contactId}")
    public Map<String, Object> deleteContact(@PathVariable Long contactId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        contactService.deleteContact(userId, contactId);
        result.put("success", true);
        return result;
    }
    
    @GetMapping("/pending")
    public Map<String, Object> getPendingRequests(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        List<Contact> requests = contactService.getPendingRequests(userId);
        result.put("success", true);
        result.put("requests", requests);
        return result;
    }
}

